package cc.minos.components.container
{
	import cc.minos.components.common.Component;
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author Minos
	 */
	public class Container extends Component
	{
		
		protected var _verticalGap:Number = 5;
		protected var _horizontalGap:Number = 5;
		
		protected var _paddingTop:Number = 0;
		protected var _paddingLeft:Number = 0;
		protected var _paddingRight:Number = 0;
		protected var _paddingBottom:Number = 0;
		
		public function Container()
		{
			super();
			this._type = 'container';
			addEventListener( Event.REMOVED_FROM_STAGE , onRemoved );
		}
		
		public function invalidate():void
		{
		}
		
		public function removeAllChildren():void
		{
			while ( numChildren > 0 )
				removeChildAt( 0 );
		}
		
		override public function addChild( child:DisplayObject ):DisplayObject
		{
			super.addChild( child );
			child.addEventListener( Event.RESIZE , onResize );
			invalidate();
			return child;
		}
		
		override public function addChildAt( child:DisplayObject , index:int ):DisplayObject
		{
			super.addChildAt( child , index );
			child.addEventListener( Event.RESIZE , onResize );
			invalidate();
			return child;
		}
		
		override public function removeChild( child:DisplayObject ):DisplayObject
		{
			super.removeChild( child );
			child.removeEventListener( Event.RESIZE , onResize );
			invalidate();
			return child;
		}
		
		override public function removeChildAt( index:int ):DisplayObject
		{
			var child:DisplayObject = super.removeChildAt( index );
			child.removeEventListener( Event.RESIZE , onResize );
			invalidate();
			return child;
		}
		
		public function get verticalGap():Number
		{
			return _verticalGap;
		}
		
		public function set verticalGap( val:Number ):void
		{
			_verticalGap = val;
			invalidate();
		}
		
		public function get horizontalGap():Number
		{
			return _horizontalGap;
		}
		
		public function set horizontalGap( val:Number ):void
		{
			_horizontalGap = val;
			invalidate();
		}
		
		public function get paddingTop():Number
		{
			return _paddingTop;
		}
		
		public function set paddingTop( val:Number ):void
		{
			_paddingTop = val;
			invalidate();
		}
		
		public function get paddingLeft():Number
		{
			return _paddingLeft;
		}
		
		public function set paddingLeft( val:Number ):void
		{
			_paddingLeft = val;
			invalidate();
		}
		
		public function get paddingRight():Number
		{
			return _paddingRight;
		}
		
		public function set paddingRight( val:Number ):void
		{
			_paddingRight = val;
			invalidate();
		}
		
		public function get paddingBottom():Number
		{
			return _paddingBottom;
		}
		
		public function set paddingBottom( val:Number ):void
		{
			_paddingBottom = val;
			invalidate();
		}
		
		override public function get width():Number
		{
			return super.width + _paddingLeft + _paddingRight;
		}
		
		override public function get height():Number
		{
			return super.height + _paddingTop + _paddingBottom;
		}
		
		protected function onResize( event:Event ):void
		{
			invalidate();
			dispatchEvent( new Event( Event.RESIZE , true ) );
		}
		
		protected function onRemoved( e:Event ):void
		{
			removeEventListener( Event.REMOVED_FROM_STAGE , onRemoved );
			removeAllChildren();
		}
	}

}